<template>
	<view class="">
		<!-- 顶部导航 -->
		<fa-navbar :title="title"></fa-navbar>
		<!-- 用户信息 -->
		<u-sticky :enable="false"><fa-user-header :user="user" :showMore="showMore"></fa-user-header></u-sticky>
		<!-- 内容 -->
		<view class="u-p-t-30 u-p-l-30 u-p-r-30">
			<view class="article-item u-p-30 u-m-b-30" v-for="(item, index) in articleList" :key="index" @click="goPage(item.id)">
				<view class="title">
					<text>{{ item.title }}</text>
				</view>
				<view class="u-m-t-20 u-flex u-flex-wrap">
					<view class="u-m-r-20 " v-for="(it, ik) in item.tags" :key="ik" @click.stop="navigateTag(it.id, 'article')">
						<u-tag
							:bg-color="theme.lightColor"
							:border-color="theme.faBorderColor"
							:color="theme.bgColor"
							:text="it.name"
							type="primary"
							shape="circle"
						/>
					</view>
				</view>
				<view class="u-m-t-30 u-flex u-row-between u-tips-color">
					<view class="">
						<u-icon name="chat"></u-icon>
						{{ item.comments }} 条评论
					</view>
					<view class="" @click.stop="collection(item)">
						<u-icon name="bookmark-fill"></u-icon>
						{{ item.collected ? '已收藏' : '收藏' }}
					</view>
					<view class="" @click.stop="thanks(item.id)">
						<u-icon name="heart-fill"></u-icon>
						感谢
					</view>

					<view class="" v-if="vuex_isAdmin || vuex_user.id == item.user_id" @click.stop="editArticle(item.id)">
						<u-icon name="edit-pen-fill"></u-icon>
						编辑
					</view>
					<view class="" @click.stop="delArticle(item.id, index)" v-if="vuex_isAdmin">
						<u-icon name="close"></u-icon>
						删除
					</view>
				</view>
			</view>
		</view>
		<!-- 加载更多 -->
		<view class="u-p-t-10 u-p-b-30" v-if="articleList.length"><u-loadmore bg-color="#f4f6f8" :status="has_more ? status : 'nomore'" /></view>
		<!-- 空数据 -->
		<wanl-empty style="padding:0;" v-if="!articleList.length"></wanl-empty>
		<!-- 感谢 -->
		<fa-thanks :source_id="source_id" type="article" ref="faThank"></fa-thanks>
		<!-- 回到顶部 -->
		<u-back-top :scroll-top="scrollTop" :icon-style="{ color: theme.bgColor }" :custom-style="{ backgroundColor: theme.lightColor }"></u-back-top>
		<!-- 底部导航 -->
		<fa-tabbar></fa-tabbar>
	</view>
</template>

<script>
import { collection, tagClick } from '@/common/fa.mixin.js';
export default {
	mixins: [collection, tagClick],
	onLoad(e) {
		let query = this.$Route.query || e || {};
		let user_id = query.user_id || 0;
		if (user_id) {
			this.title = 'TA的文章';
			uni.setNavigationBarTitle({
				title: this.title
			});
			this.showMore = true;
			this.user = this.vuex_othen_user;
		} else {
			this.user = this.vuex_user;
		}
	},
	onShow() {
		this.page = 1;
		this.is_update = true;
		this.getUserArticle();
	},
	data() {
		return {
			title: '我的文章',
			showMore: false,
			source_id: 0,
			user: {},
			scrollTop: 0,
			articleList: [],
			page: 1,
			has_more: false,
			status: 'loadmore',
			is_update:false
		};
	},
	methods: {
		getUserArticle: async function() {
			let res = await this.$api.getUserArticle({
				id: this.user.id,
				page: this.page
			});
			this.status = 'loadmore';
			if (!res.code) {
				this.$u.toast(res.msg);
				return;
			}
			if(this.is_update){
				this.articleList = [];
				this.is_update = false;
			}
			this.articleList = this.articleList.concat(res.data.data);
			this.has_more = res.data.last_page < res.data.current_page;
		},
		goPage(id) {
			this.$wanlshop.to(`/pages/community/article/detail?id=${id}`);
		},
		//收藏
		collection(item) {
			if (item.collected) {
				this.goCollectionDelete(item, 'article');
			} else {
				this.goCollectionCreate(item, 'article');
			}
		},
		//感谢
		thanks(id) {
			this.source_id = id;
			this.$refs.faThank.show();
		},
		//编辑文章
		editArticle(id) {
			this.$wanlshop.to(`/pages/community/article/post?id=${id}&type=update`);
		},
		delArticle: async function(id, index) {
			let res = await this.$api.getArticleDelete({
				id: id
			});
			this.$u.toast(res.msg);
			this.articleList.splice(index, 1);
		}
	},
	onPageScroll(e) {
		this.scrollTop = e.scrollTop;
	},
	onReachBottom() {
		if (this.has_more) {
			this.status = 'loading';
			this.page++;
			this.getUserArticle();
		}
	}
};
</script>

<style>
page {
	background-color: #f4f6f8;
}

.article-item {
	background: #ffffff;
	box-shadow: 0 0 5px rgba(0, 134, 243, 0.1);
	border-radius: 3px;
}
</style>
